package com.jack006.article.controller;

import com.jack006.api.BaseController;
import com.jack006.api.controller.article.CommentControllerApi;
import com.jack006.api.controller.user.UserControllerApi;
import com.jack006.article.service.CommentPortalService;
import com.jack006.grace.result.GraceJSONResult;
import com.jack006.pojo.bo.CommentReplyBO;
import com.jack006.pojo.vo.AppUserVO;
import com.jack006.utils.JsonUtils;
import com.jack006.utils.PagedGridResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Classname CommentController
 * @Description 文章留言控制类
 * @Date 2021-06-10 08:15
 * @Created by jack
 */
@RestController
public class CommentController extends BaseController implements CommentControllerApi {

    final static Logger logger = LoggerFactory.getLogger(CommentController.class);


    @Autowired
    private CommentPortalService commentPortalService;
    @Resource
    private UserControllerApi userControllerApi;

    @Override
    public GraceJSONResult createComment(@Valid CommentReplyBO commentReplyBO) {


        // 1.根据留言查询他的昵称，用于存入到数据表进行字段冗余处理，
        // 从而避免多表管理查询的性能影响
        String userId = commentReplyBO.getCommentUserId();

        // 2.发起redisTemplate调用用户服务，用户获取用户昵称
        Set<String> idSet = new HashSet<>();
        idSet.add(userId);
         List<AppUserVO> appUserVOList =getBasicUserList(idSet);
         String nickName = appUserVOList.get(0).getNickname();
        String userFace = appUserVOList.get(0).getFace();

        commentPortalService.createComments(commentReplyBO.getArticleId(),
                                            commentReplyBO.getFatherId(),
                                            commentReplyBO.getContent(),
                                            userId,
                                            nickName,
                                            userFace);
        return GraceJSONResult.ok();
    }



    /**
     * 发起远程调用，获得用户的基本信息
     * @param idSet
     * @return
     */
    public List<AppUserVO> getBasicUserList(Set<String> idSet) {
//        String serviceId = "SERVICE-USER";
//        String userServerUrlExecute = "http://"+serviceId+"/user/queryByIds?userIds="+ JsonUtils.objectToJson(idSet);
//        ResponseEntity<GraceJSONResult> responseEntity = restTemplate.getForEntity(userServerUrlExecute, GraceJSONResult.class);
//        GraceJSONResult bodyResult = responseEntity.getBody();
        GraceJSONResult bodyResult = userControllerApi.queryByIds(JsonUtils.objectToJson(idSet));
        List<AppUserVO> publisherList = null;
        if (bodyResult.getStatus() == 200) {
            String userJson = JsonUtils.objectToJson(bodyResult.getData());
            publisherList = JsonUtils.jsonToList(userJson, AppUserVO.class);
        } else {
            publisherList = new ArrayList<>();
        }
        return publisherList;
    }

    @Override
    public GraceJSONResult commentCounts(String articleId) {
        Integer counts = getCountsFormRedis(REDIS_ARTICLE_COMMENTS_COUNTS + ":" + articleId);
        return GraceJSONResult.ok(counts);
    }

    @Override
    public GraceJSONResult list(String articleId, Integer page, Integer pageSize) {
        if (page == null) {
            page = COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult gridResult = commentPortalService.queryArticleComments(articleId,page,pageSize);
        return GraceJSONResult.ok(gridResult);
    }

    @Override
    public GraceJSONResult delete(String writerId, String commentId) {
        commentPortalService.deleteComment(writerId, commentId);
        return GraceJSONResult.ok();
    }

    @Override
    public GraceJSONResult mng(String writerId, Integer page, Integer pageSize) {
        if (page == null) {
            page = COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult gridResult = commentPortalService.queryWriterCommentsMng(writerId, page, pageSize);
        return GraceJSONResult.ok(gridResult);
    }
}
